{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# numpy.array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "'1.16.5'"
     },
     "metadata": {},
     "execution_count": 2
    }
   ],
   "source": [
    "numpy.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "'1.16.5'"
     },
     "metadata": {},
     "execution_count": 4
    }
   ],
   "source": [
    "np.__version__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python List"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
     },
     "metadata": {},
     "execution_count": 5
    }
   ],
   "source": [
    "L = [i for i in range(10)]\n",
    "L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "5"
     },
     "metadata": {},
     "execution_count": 7
    }
   ],
   "source": [
    "L[5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[0, 1, 2, 3, 4, 100, 6, 7, 8, 9]"
     },
     "metadata": {},
     "execution_count": 8
    }
   ],
   "source": [
    "L[5] = 100\n",
    "L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array('i', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
     },
     "metadata": {},
     "execution_count": 10
    }
   ],
   "source": [
    "arr = array.array('i' , [i for i in range(10)])\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "5"
     },
     "metadata": {},
     "execution_count": 12
    }
   ],
   "source": [
    "arr[5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr[5] = 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array('i', [0, 1, 2, 3, 4, 100, 6, 7, 8, 9])"
     },
     "metadata": {},
     "execution_count": 14
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "output_type": "error",
     "ename": "TypeError",
     "evalue": "an integer is required (got type str)",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-15-343c9c5d73a2>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0marr\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'Machine'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: an integer is required (got type str)"
     ]
    }
   ],
   "source": [
    "arr[5] = 'Machine'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
     },
     "metadata": {},
     "execution_count": 16
    }
   ],
   "source": [
    "nparr = np.array([i for i in range(10)])\n",
    "nparr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "5"
     },
     "metadata": {},
     "execution_count": 17
    }
   ],
   "source": [
    "nparr[5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "dtype('int32')"
     },
     "metadata": {},
     "execution_count": 19
    }
   ],
   "source": [
    "nparr.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "nparr[5] = 5.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
     },
     "metadata": {},
     "execution_count": 21
    }
   ],
   "source": [
    "nparr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "nparr[3] = 3.14"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
     },
     "metadata": {},
     "execution_count": 23
    }
   ],
   "source": [
    "nparr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "nparr2 = np.array([1,2,3.0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "dtype('float64')"
     },
     "metadata": {},
     "execution_count": 25
    }
   ],
   "source": [
    "nparr2.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([1., 2., 3.])"
     },
     "metadata": {},
     "execution_count": 26
    }
   ],
   "source": [
    "nparr2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 其他创建numpy.array的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])"
     },
     "metadata": {},
     "execution_count": 27
    }
   ],
   "source": [
    "np.zeros(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])"
     },
     "metadata": {},
     "execution_count": 28
    }
   ],
   "source": [
    "np.zeros(10, dtype=int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[0., 0., 0., 0., 0.],\n       [0., 0., 0., 0., 0.],\n       [0., 0., 0., 0., 0.]])"
     },
     "metadata": {},
     "execution_count": 29
    }
   ],
   "source": [
    "np.zeros((3,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[0, 0, 0, 0, 0],\n       [0, 0, 0, 0, 0],\n       [0, 0, 0, 0, 0]])"
     },
     "metadata": {},
     "execution_count": 30
    }
   ],
   "source": [
    "np.zeros(shape=(3,5), dtype=int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[1., 1., 1., 1., 1.],\n       [1., 1., 1., 1., 1.],\n       [1., 1., 1., 1., 1.]])"
     },
     "metadata": {},
     "execution_count": 31
    }
   ],
   "source": [
    "np.ones((3,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[66, 66, 66, 66, 66],\n       [66, 66, 66, 66, 66],\n       [66, 66, 66, 66, 66]])"
     },
     "metadata": {},
     "execution_count": 33
    }
   ],
   "source": [
    "np.full(shape=(3,5),fill_value=66 )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## arange"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]"
     },
     "metadata": {},
     "execution_count": 34
    }
   ],
   "source": [
    "[i for i in range(0,20,2)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])"
     },
     "metadata": {},
     "execution_count": 35
    }
   ],
   "source": [
    "np.arange(0,20,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "output_type": "error",
     "ename": "TypeError",
     "evalue": "'float' object cannot be interpreted as an integer",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-36-467c61f72d62>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m20\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0.2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: 'float' object cannot be interpreted as an integer"
     ]
    }
   ],
   "source": [
    "[i for i in range(0,20,0.2)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([ 0. ,  0.2,  0.4,  0.6,  0.8,  1. ,  1.2,  1.4,  1.6,  1.8,  2. ,\n        2.2,  2.4,  2.6,  2.8,  3. ,  3.2,  3.4,  3.6,  3.8,  4. ,  4.2,\n        4.4,  4.6,  4.8,  5. ,  5.2,  5.4,  5.6,  5.8,  6. ,  6.2,  6.4,\n        6.6,  6.8,  7. ,  7.2,  7.4,  7.6,  7.8,  8. ,  8.2,  8.4,  8.6,\n        8.8,  9. ,  9.2,  9.4,  9.6,  9.8, 10. , 10.2, 10.4, 10.6, 10.8,\n       11. , 11.2, 11.4, 11.6, 11.8, 12. , 12.2, 12.4, 12.6, 12.8, 13. ,\n       13.2, 13.4, 13.6, 13.8, 14. , 14.2, 14.4, 14.6, 14.8, 15. , 15.2,\n       15.4, 15.6, 15.8, 16. , 16.2, 16.4, 16.6, 16.8, 17. , 17.2, 17.4,\n       17.6, 17.8, 18. , 18.2, 18.4, 18.6, 18.8, 19. , 19.2, 19.4, 19.6,\n       19.8])"
     },
     "metadata": {},
     "execution_count": 37
    }
   ],
   "source": [
    "np.arange(0,20,0.2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## linspace 终止点包含"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([ 0.        ,  2.22222222,  4.44444444,  6.66666667,  8.88888889,\n       11.11111111, 13.33333333, 15.55555556, 17.77777778, 20.        ])"
     },
     "metadata": {},
     "execution_count": 38
    }
   ],
   "source": [
    "np.linspace(0,20,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([ 0.,  2.,  4.,  6.,  8., 10., 12., 14., 16., 18., 20.])"
     },
     "metadata": {},
     "execution_count": 39
    }
   ],
   "source": [
    "np.linspace(0,20,11)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([4, 9, 0, 6, 0, 9, 7, 4, 8, 2])"
     },
     "metadata": {},
     "execution_count": 40
    }
   ],
   "source": [
    "np.random.randint(0,10,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[6, 4, 7, 5, 4],\n       [6, 6, 5, 4, 4],\n       [6, 7, 6, 4, 6]])"
     },
     "metadata": {},
     "execution_count": 41
    }
   ],
   "source": [
    "np.random.randint(4,8,size=(3,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "<function RandomState.seed>"
     },
     "metadata": {},
     "execution_count": 43
    }
   ],
   "source": [
    "np.random.seed(666)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[5, 7, 6, 5, 4],\n       [5, 7, 6, 6, 4],\n       [6, 7, 4, 6, 7]])"
     },
     "metadata": {},
     "execution_count": 44
    }
   ],
   "source": [
    "np.random.randint(4,8,size=(3,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[4, 6, 5, 6, 6],\n       [6, 5, 6, 4, 5],\n       [7, 6, 7, 4, 7]])"
     },
     "metadata": {},
     "execution_count": 45
    }
   ],
   "source": [
    "np.random.seed(666)\n",
    "np.random.randint(4,8,size=(3,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[4, 6, 5, 6, 6],\n       [6, 5, 6, 4, 5],\n       [7, 6, 7, 4, 7]])"
     },
     "metadata": {},
     "execution_count": 46
    }
   ],
   "source": [
    "np.random.seed(666)\n",
    "np.random.randint(4,8,size=(3,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[0.28116849, 0.46284169, 0.23340091, 0.76706421, 0.81995656],\n       [0.39747625, 0.31644109, 0.15551206, 0.73460987, 0.73159555],\n       [0.8578588 , 0.76741234, 0.95323137, 0.29097383, 0.84778197]])"
     },
     "metadata": {},
     "execution_count": 48
    }
   ],
   "source": [
    "\n",
    "np.random.random(size=(3,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0.6136098151346036"
     },
     "metadata": {},
     "execution_count": 49
    }
   ],
   "source": [
    "np.random.normal()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "-11.747638364264734"
     },
     "metadata": {},
     "execution_count": 50
    }
   ],
   "source": [
    "np.random.normal(10,100)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[ 0.22196962, -1.86735182, -0.25584759, -1.76438083, -0.94249688],\n       [-1.58045861,  0.90472662, -0.82628327,  0.82101369,  0.36712592],\n       [ 1.65399586,  0.13946473, -1.21715355, -0.99494737, -1.56448586]])"
     },
     "metadata": {},
     "execution_count": 51
    }
   ],
   "source": [
    "np.random.normal(0,1,(3,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "text": "\u001b[1;31mDocstring:\u001b[0m\nnormal(loc=0.0, scale=1.0, size=None)\n\nDraw random samples from a normal (Gaussian) distribution.\n\nThe probability density function of the normal distribution, first\nderived by De Moivre and 200 years later by both Gauss and Laplace\nindependently [2]_, is often called the bell curve because of\nits characteristic shape (see the example below).\n\nThe normal distributions occurs often in nature.  For example, it\ndescribes the commonly occurring distribution of samples influenced\nby a large number of tiny, random disturbances, each with its own\nunique distribution [2]_.\n\nParameters\n----------\nloc : float or array_like of floats\n    Mean (\"centre\") of the distribution.\nscale : float or array_like of floats\n    Standard deviation (spread or \"width\") of the distribution.\nsize : int or tuple of ints, optional\n    Output shape.  If the given shape is, e.g., ``(m, n, k)``, then\n    ``m * n * k`` samples are drawn.  If size is ``None`` (default),\n    a single value is returned if ``loc`` and ``scale`` are both scalars.\n    Otherwise, ``np.broadcast(loc, scale).size`` samples are drawn.\n\nReturns\n-------\nout : ndarray or scalar\n    Drawn samples from the parameterized normal distribution.\n\nSee Also\n--------\nscipy.stats.norm : probability density function, distribution or\n    cumulative density function, etc.\n\nNotes\n-----\nThe probability density for the Gaussian distribution is\n\n.. math:: p(x) = \\frac{1}{\\sqrt{ 2 \\pi \\sigma^2 }}\n                 e^{ - \\frac{ (x - \\mu)^2 } {2 \\sigma^2} },\n\nwhere :math:`\\mu` is the mean and :math:`\\sigma` the standard\ndeviation. The square of the standard deviation, :math:`\\sigma^2`,\nis called the variance.\n\nThe function has its peak at the mean, and its \"spread\" increases with\nthe standard deviation (the function reaches 0.607 times its maximum at\n:math:`x + \\sigma` and :math:`x - \\sigma` [2]_).  This implies that\n`numpy.random.normal` is more likely to return samples lying close to\nthe mean, rather than those far away.\n\nReferences\n----------\n.. [1] Wikipedia, \"Normal distribution\",\n       https://en.wikipedia.org/wiki/Normal_distribution\n.. [2] P. R. Peebles Jr., \"Central Limit Theorem\" in \"Probability,\n       Random Variables and Random Signal Principles\", 4th ed., 2001,\n       pp. 51, 51, 125.\n\nExamples\n--------\nDraw samples from the distribution:\n\n>>> mu, sigma = 0, 0.1 # mean and standard deviation\n>>> s = np.random.normal(mu, sigma, 1000)\n\nVerify the mean and the variance:\n\n>>> abs(mu - np.mean(s)) < 0.01\nTrue\n\n>>> abs(sigma - np.std(s, ddof=1)) < 0.01\nTrue\n\nDisplay the histogram of the samples, along with\nthe probability density function:\n\n>>> import matplotlib.pyplot as plt\n>>> count, bins, ignored = plt.hist(s, 30, density=True)\n>>> plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) *\n...                np.exp( - (bins - mu)**2 / (2 * sigma**2) ),\n...          linewidth=2, color='r')\n>>> plt.show()\n\u001b[1;31mType:\u001b[0m      builtin_function_or_method\n"
    }
   ],
   "source": [
    "np.random.normal?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}